from flask import request
from flask_restful import Resource, reqparse
from exts import api, db
from flask_docs import ApiDoc
from services.warehouse_service import *
from utils.auth import auth_service
from flask import jsonify
from models.user import AdminUser, RoleType


class warehouses(Resource):
    @auth_service().require_role(RoleType.SUPER)
    @auth_service().verify_token
    def get(self, id=None):
        try:
            if id:
                return {
                    "message": "查询成功",
                    "warehouse": warehouse_service().get_id_warehouse(id=id),
                    "code": 200  # 添加 code 字段
                }, 200
            else:
                if warehouse_service().get_all_warehouses() == []:
                    return {
                        "message": "查询成功,无仓库信息",
                        "warehouses": [],
                        "code": 200
                    }, 200
                return {
                    "message": "查询成功",
                    "warehouses": warehouse_service().get_all_warehouses(),
                    "code": 200
                }, 200
        except Exception as e:
            return {"message": str(e), "code": 400}, 400  # 添加 code 字段
        finally:
            db.session.close()

    @auth_service().require_role(RoleType.SUPER)
    @auth_service().verify_token
    def post(self):
        try:
            post_json = request.get_json()
            user_id = auth_service().get_user_id_from_token()

            if post_json:
                warehouses_info = warehouse_service().create_warehouse(
                    post_json, user_id
                )
                return {
                    "message": f"仓库{warehouses_info['name']}创建成功",
                    "warehouse": warehouses_info,
                    "code": 200  # 添加 code 字段
                }, 200
            else:
                return {"message": "请输入仓库信息", "code": 400}, 400  # 添加 code 字段
        except Exception as e:
            return {"message": str(e), "code": 400}, 400  # 添加 code 字段
        finally:
            db.session.close()

    @auth_service().require_role(RoleType.SUPER)
    @auth_service().verify_token
    def put(self):
        try:
            post_json = request.get_json()
            print('put',post_json)
            if post_json:
                warehouses_info = warehouse_service().update_warehouse(
                    put_json=post_json
                )
                return {
                    "message": f"仓库{warehouses_info['name']}更新成功",
                    "warehouse": warehouses_info,
                    "code": 200  # 添加 code 字段
                }, 200
            else:
                return {"message": "请输入仓库信息", "code": 400}, 400  # 添加 code 字段
        except Exception as e:
            return {"message": str(e), "code": 400}, 400  # 添加 code 字段
        finally:
            db.session.close()

    @auth_service().require_role(RoleType.SUPER)
    @auth_service().verify_token
    def delete(self):
        try:
            delete_json = request.get_json()
            if delete_json:
                warehouses_info = warehouse_service().delete_warehouse(
                    delete_json=delete_json
                )
                return {
                    "message": f"仓库{warehouses_info['name']}删除成功",
                    "warehouse": warehouses_info,
                    "code": 200  # 添加 code 字段
                }, 200
            else:
                return {"message": "请输入要删除的仓库信息", "code": 400}, 400  # 添加缺失的 else 分支
        except Exception as e:
            return {"message": str(e), "code": 400}, 400  # 添加 code 字段
        finally:
            db.session.close()


api.add_resource(warehouses, "/api/warehouses", "/api/warehouses/<int:id>")
